﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;


namespace OficinaTuristicaFE
{
    /// <summary>
    /// Interaction logic for MainMenu.xaml
    /// </summary>
    public partial class MainMenu : SurfaceWindow 
    {
        /// <summary>
        /// Variable to load just one page
        /// </summary>
        bool isLoading;

        Storyboard backgroundAnimation;
        Storyboard languageAnimation;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public MainMenu()
        {
            InitializeComponent();

            // Add handlers for Application activation events
            AddActivationHandlers();
            backgroundAnimation = (Storyboard)this.Resources["BackgroundAnimation"];
            languageAnimation = (Storyboard)this.Resources["FaddeInLanguajes"];
            //backgroundAnimation.Begin();
        }

        //protected override void OnInitialized(EventArgs e)
        //{
        //    base.OnInitialized(e);
        //}


        /// <summary>
        /// Occurs when the window is about to close. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            // Remove handlers for Application activation events
            RemoveActivationHandlers();
        }

        /// <summary>
        /// Adds handlers for Application activation events.
        /// </summary>
        private void AddActivationHandlers()
        {
            // Subscribe to surface application activation events
            ApplicationLauncher.ApplicationActivated += OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed += OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated += OnApplicationDeactivated;
        }

        /// <summary>
        /// Removes handlers for Application activation events.
        /// </summary>
        private void RemoveActivationHandlers()
        {
            // Unsubscribe from surface application activation events
            ApplicationLauncher.ApplicationActivated -= OnApplicationActivated;
            ApplicationLauncher.ApplicationPreviewed -= OnApplicationPreviewed;
            ApplicationLauncher.ApplicationDeactivated -= OnApplicationDeactivated;
        }

        /// <summary>
        /// This is called when application has been activated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationActivated(object sender, EventArgs e)
        {
            //TODO: enable audio, animations here
            backgroundAnimation.Begin();
            backgroundAnimation.RepeatBehavior = RepeatBehavior.Forever;
        }

        /// <summary>
        /// This is called when application is in preview mode.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationPreviewed(object sender, EventArgs e)
        {
            //TODO: Disable audio here if it is enabled
            backgroundAnimation.Stop();
           
            //TODO: optionally enable animations here
        }

        /// <summary>
        ///  This is called when application has been deactivated.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnApplicationDeactivated(object sender, EventArgs e)
        {
            //TODO: disable audio, animations here
            backgroundAnimation.Stop();
        }

        private void Catala_Click(object sender, RoutedEventArgs e)
        {
            showServiceList("/LanguageResources/Catalan.xaml", 0);        
        }

        private void Castella_Click(object sender, RoutedEventArgs e)
        {
            showServiceList("/LanguageResources/Spanish.xaml", 1);
        }

        private void Angles_Click(object sender, RoutedEventArgs e)
        {
            showServiceList("/LanguageResources/English.xaml", 2);
        }

        private void Aleman_Click(object sender, RoutedEventArgs e)
        {
            showServiceList("/LanguageResources/German.xaml", 3);
        }

        object serviceListLock = new object();
        ServiceList serviceList;

        private void showServiceList(string url, int languageItem)
        {
            if (serviceList == null)
            {
                lock (serviceListLock)
                {
                    if (serviceList == null)
                    {
                        App.FirstTime = 0;
                        App.Language = new Uri(url, UriKind.Relative);
                        App.LanguageItem = languageItem;
                        serviceList = new ServiceList();
                        serviceList.Closed += new EventHandler(serviceList_Closed);
                        serviceList.Show();
                        backgroundAnimation.Stop();
                        this.Hide();
                        //backgroundAnimation.Stop();
                    }
                }
                //new PublicPrivateSelection().ShowDialog();
            }
            
        }

        void serviceList_Closed(object sender, EventArgs e)
        {
            lock(serviceListLock)
            {
                serviceList = null;
                this.Show();
                backgroundAnimation.Begin();
                languageAnimation.Begin();
            }
        }

        private void Catala_ContactTapGesture(object sender, ContactEventArgs e)
        {
            showServiceList("/LanguageResources/Catalan.xaml", 0); 

            //if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/Catalan.xaml", UriKind.Relative);
            //    App.LanguageItem = 0;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;
            //}
        }

        private void Castella_ContactTapGesture(object sender, ContactEventArgs e)
        {
            showServiceList("/LanguageResources/Spanish.xaml", 1); 
            //if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/Spanish.xaml", UriKind.Relative);
            //    App.LanguageItem = 1;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }

        private void Angles_ContactTapGesture(object sender, ContactEventArgs e)
        {
            showServiceList("/LanguageResources/English.xaml", 2); 
            //if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/English.xaml", UriKind.Relative);
            //    App.LanguageItem = 2;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }

        private void Aleman_ContactTapGesture(object sender, ContactEventArgs e)
        {
            showServiceList("/LanguageResources/German.xaml", 3); 
            //if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/German.xaml", UriKind.Relative);
            //    App.LanguageItem = 3;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }

        private void ScatterCatalan_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            showServiceList("/LanguageResources/Catalan.xaml", 0); 
            // if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/Catalan.xaml", UriKind.Relative);
            //    App.LanguageItem = 0;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;
            //}
        }

        private void ScatterSpanish_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            showServiceList("/LanguageResources/Spanish.xaml", 1); 
            //if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/Spanish.xaml", UriKind.Relative);
            //    App.LanguageItem = 1;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }

        private void ScatterEnglish_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            showServiceList("/LanguageResources/English.xaml", 2); 
            // if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/English.xaml", UriKind.Relative);
            //    App.LanguageItem = 2;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }

        private void ScatterGerman_ContactTapGesture(object sender, Microsoft.Surface.Presentation.ContactEventArgs e)
        {
            showServiceList("/LanguageResources/German.xaml", 3); 
            // if (isLoading == false)
            //{
            //    isLoading = true;
            //    App.FirstTime = 0;
            //    App.Language = new Uri("/LanguageResources/German.xaml", UriKind.Relative);
            //    App.LanguageItem = 3;
            //    new ServiceList().ShowDialog();
            //    isLoading = false;

            //}
        }
            
    }
}